var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd';

function padding(s, len) {
	//	console.log(s, len)
	var len = len - (s + '').length;
	for(var i = 0; i < len; i++) {
		s = '0' + s;
	}
	return s;
};


export default {
	/*封装时间格式转换  如：时间戳 1501264588 = 2001-01-01 01：01：*/
	formatDateTime: function(timeStamp) {
	 var date = new Date();  
    date.setTime(timeStamp * 1000);  
    var y = date.getFullYear();      
    var m = date.getMonth() + 1;      
    m = m < 10 ? ('0' + m) : m;      
    var d = date.getDate();      
    d = d < 10 ? ('0' + d) : d;      
    var h = date.getHours();    
    h = h < 10 ? ('0' + h) : h;    
    var minute = date.getMinutes();    
    var second = date.getSeconds();    
    minute = minute < 10 ? ('0' + minute) : minute;      
    second = second < 10 ? ('0' + second) : second;     
    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;  
	},
	/*封装时间格式转换  如：20010101010101 = 2001-01-01 01：01：*/
	newFormatDate: function(value, pattern) {
		if(value == null) {
			return '';
		};
		pattern = pattern || DEFAULT_PATTERN;
		return pattern.replace(SIGN_REGEXP, function($0) {
			switch($0.charAt(0)) {
				case 'y':
					return value.substr(0, 4);
				case 'M':
					return value.substr(4, 2);
				case 'd':
					return value.substr(6, 2);
				case 'h':
					return value.substr(8, 2);
				case 'm':
					return value.substr(10, 2);
				case 's':
					return value.substr(12, 2);
			}
		});
	},
	delHtmlTag: function(b) {
		var a = b.replace(/<[^>]+>/g, "");
		a = a.replace(/&ensp;/g, "");
		a = a.replace(/&emsp;/g, "");
		a = a.replace(/&nbsp;/g, "");
		a = a.replace(/&lt;/g, "");
		a = a.replace(/&gt;/g, "");
		a = a.replace(/&amp;/g, "");
		a = a.replace(/&quot;/g, "");
		a = a.replace(/&copy;/g, "");
		a = a.replace(/&reg;/g, "");
		a = a.replace(/&times;/g, "");
		a = a.replace(/&divide;/g, "");
		return a;
	},
	getQueryStringByName: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		var context = "";
		if(r != null)
			context = r[2];
		reg = null;
		r = null;
		return context == null || context == "" || context == "undefined" ? "" : context;
	},
	formatDate: {
		format: function(date, pattern) {
			pattern = pattern || DEFAULT_PATTERN;
			return pattern.replace(SIGN_REGEXP, function($0) {
				switch($0.charAt(0)) {
					case 'y':
						return padding(date.getFullYear(), $0.length);
					case 'M':
						return padding(date.getMonth() + 1, $0.length);
					case 'd':
						return padding(date.getDate(), $0.length);
					case 'w':
						return date.getDay() + 1;
					case 'h':
						return padding(date.getHours(), $0.length);
					case 'm':
						return padding(date.getMinutes(), $0.length);
					case 's':
						return padding(date.getSeconds(), $0.length);
				}
			});
		},
		parse: function(dateString, pattern) {
			var matchs1 = pattern.match(SIGN_REGEXP);
			var matchs2 = dateString.match(/(\d)+/g);
			if(matchs1.length == matchs2.length) {
				var _date = new Date(1970, 0, 1);
				for(var i = 0; i < matchs1.length; i++) {
					var _int = parseInt(matchs2[i]);
					var sign = matchs1[i];
					switch(sign.charAt(0)) {
						case 'y':
							_date.setFullYear(_int);
							break;
						case 'M':
							_date.setMonth(_int - 1);
							break;
						case 'd':
							_date.setDate(_int);
							break;
						case 'h':
							_date.setHours(_int);
							break;
						case 'm':
							_date.setMinutes(_int);
							break;
						case 's':
							_date.setSeconds(_int);
							break;
					}
				}
				return _date;
			}
			return null;
		}

	},
	formatMoney: function(value) {
		var obj = {
			int: undefined, //整数位  
			dec: undefined, //小数位  
			targ: "", //正负  
			times: ['', '万', '亿', '万亿', '亿亿']
		}
		value = String(value);
		var reg = /^-?\d+\.?\d+$/;
		if(!reg.test(value)) {
			return false;
		}
		if(value[0] == "-") {
			obj.targ = "-";
			value = value.substring(1, value.length)
		}

		var times = 0;
		value = Number(value);
		while(value > 10000) {
			value = value / 10000;
			times++;
		}
		value = value.toFixed(2)
		var arr = String(value).split(".")
		obj.dec = arr[1];
		obj.int = arr[0];
		if(obj.int.length > 3) {
			obj.int = obj.int.replace(/(.{1})/, '$1,')
		}
		return obj.targ + obj.int + "." + obj.dec + obj.times[times];
	},
};